package eu.tsp.isensors.communication.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;

import java.util.Map;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Context;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author wdrira
 */
@Path("sensors")

public class Node {
 	
	ServicesInvoke si = ServicesInvoke.getInstance();
   
	@GET
  	public String get(@Context UriInfo ui) {
		MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
		MultivaluedMap<String, String> pathParams = ui.getPathParameters();
		printMap(queryParams);
		System.out.println ("** "+ui.getBaseUri().toString()+"*1*"+ui.getBaseUri().getHost() +"*2*"+ui.getBaseUri().getPath());
		//printMap(pathParams);
		return getNodeID(ui);
	}
/*
	@GET @Produces("text/plain")	
	public String getStatus() {
        	return "active";
	}

	@PUT 
	@Path("/{sensorUid}")
	public String putInSensorUid(byte[] BO, @PathParam("sensorUid") String sensorID, @Context UriInfo ui )
	{
		return "PUT "+getNodeID(ui)+" "+sensorID;
	}
*/
	@GET
        @Path("/{sensorUid}")
        public String getFromSensorUid(@PathParam("sensorUid") String sensorID, @Context UriInfo ui )
        {
		//si.invokeGet();
                return "GET "+getNodeID(ui)+" "+sensorID+"\n";//+si.invokeGetString("_db_www_scripts_main.js");
        }
	
	@GET
        @Path("/{sensorUid}/{id}")
        public String getIDFromSensorUid(@PathParam("sensorUid") String sensorID, @PathParam("id") String id, @Context UriInfo ui )
        {
                return si.invokeGetString(id);
        }	
	
	@PUT
        @Path("/{sensorUid}")
        public String putInSensorUid(byte[] BO, @PathParam("sensorUid") String sensorID, @Context UriInfo ui )
        {
                return si.invokePut("/db/isensors/"+getNodeID(ui)+"/sensors/"+sensorID, "", BO, "");
        }

	@PUT
        @Path("/{sensorUid}/{name}")
        public String putInSensorUidWithName(byte[] BO, @PathParam("sensorUid") String sensorID, @PathParam("name") String name, @Context UriInfo ui )
        {
                return si.invokePut("/db/isensors/"+getNodeID(ui)+"/sensors/"+sensorID, "", BO, name);
        }
	
	@GET
        @Path("/{sensorUid}/channels")
        public String getChannels(@PathParam("sensorUid") String sensorID, @Context UriInfo ui )
        {
                return "GET "+getNodeID(ui)+" "+sensorID+" CHANNELS \n";
        }

	@GET
        @Path("/{sensorUid}/channels/{channelid}")
        public String getChannelId(@PathParam("sensorUid") String sensorID, @PathParam("channelid") String channelid, @Context UriInfo ui )
        {
                return "GET "+getNodeID(ui)+" "+sensorID+" Channels " + channelid;
        }

	@GET
        @Path("/{sensorUid}/channels/{channelid}/{id}")
        public String getIdFromChannelId(@PathParam("sensorUid") String sensorID, @PathParam("channelid") String channelid, @PathParam("id") String id, @Context UriInfo ui )
        {
                return si.invokeGetString(id);
        }

	public void printMap(MultivaluedMap<String, String> m){
		Set set= m.keySet (  ) ; 
     		Iterator iter = set.iterator (  ) ; 
     		int i=1; 
     		while ( iter.hasNext (  )  )  {  
       			System.out.println ( " "+i+" )  "+ m.get ( iter.next (  )  )  ) ; 
       			i++; 
      		}  
	}

	private String getNodeID(UriInfo ui){
		int lng = ui.getBaseUri().getPath().split("/").length;
                return ui.getBaseUri().getPath().split("/")[lng -1];
	}
}
